package com.act;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;


/**
 * 推送的后台服务
 * 
 * @author Administrator
 *
 */
public class NotificationService extends Service {

	private static final String TAG = LogUtil.getClassName(NotificationService.class);
	
	private NotificationReceiver notificationReceiver = null;
	private NotificationConnectReceiver notificationConnect = null;
	
	private TelephonyManager telephonyManager = null;
	private PhoneStateChangeListener phoneListener = null;
	
	public NotificationService()
	{
		notificationReceiver = new NotificationReceiver();
		notificationConnect = new NotificationConnectReceiver();
		phoneListener = new PhoneStateChangeListener(this);
		telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(TAG, "NotificationService.. onCreate..is running");
		
		registNotificationReceiver();
		registNotificationConnect();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "NotificationService.. onDestroy ..is running");
		
		unregistNotificationReceiver();
		unregisterNotificationConnect();
	}

	private void registNotificationReceiver()
	{
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constants.ACTION_NOTIFICATION_SHOW);
		filter.addAction(Constants.ACTION_NOTIFICATION_CLICK);
		filter.addAction(Constants.ACTION_NOTIFICATION_CLEAR);
		registerReceiver(notificationReceiver, filter);
	}
	
	private void unregistNotificationReceiver()
	{
		unregisterReceiver(notificationReceiver);
	}
	
	private void registNotificationConnect()
	{
		telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
		IntentFilter filter = new IntentFilter();
		filter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(notificationConnect, filter);
	}
	
	private void unregisterNotificationConnect()
	{
		unregisterReceiver(notificationConnect);
	}
	
	/**
	 * 获取服务器的Intent
	 * @return
	 */
	public Intent getIntent()
	{
		return new Intent().setAction("android.com.notificatoin_service");
	}
	
	/**
	 * 重新连接
	 */
	public void reconnect() {
		
	}
	
	/**
	 * 连接
	 */
	public void connect(){
		
	}
}
